package com.c.ctgproject.ui.storage.material

import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.c.ctgproject.R
import com.c.ctgproject.adapter.storage.StorageMaterialOutAdapter
import com.c.ctgproject.base.BaseActivity
import com.c.ctgproject.entity.storage.StorageMaterialOutEntity
import com.c.ctgproject.entity.storage.StorageMaterialOutListEntity
import com.c.ctgproject.utils.*
import kotlinx.android.synthetic.main.activity_storage_material_out.*

/**
 *作者:LiBW
 *创建日期: 2020/9/15
 *描述:仓储 物料出库页面
 */
class StorageMaterialOutActivity : BaseActivity() {
    private var page = 1
    private var size = 5
    override fun getLayout() = R.layout.activity_storage_material_out
    private val mList = ArrayList<StorageMaterialOutEntity.DataBean.ListBean>()
    private val mAdapter: StorageMaterialOutAdapter by lazy { StorageMaterialOutAdapter(this, mList) }

    private val mViewModel: StorageMaterialOutViewModel by lazy { ViewModelProvider(this)[StorageMaterialOutViewModel::class.java] }
    override fun event() {
        changeTransparentStatusBar()
        mStorageRV.adapter = mAdapter
        mStorageRV.layoutManager = LinearLayoutManager(this)
        mViewModel.detail.observe(this, Observer {
            mRefresh.over()
            loading.dismiss()
            MyLog.d("物料出库列表回调:$it")
            it.handlerCall({
                val entity = getGson().fromJson(it, StorageMaterialOutEntity::class.java).data
                entity?.list?.let { list ->
                    mList.addAll(list)
                    mAdapter.notifyDataSetChanged()
                }
            }, { error ->
                showToast(error)
            })
        })

        mViewModel.error.observe(this, Observer {
            mRefresh.over()
            MyLog.d("物料出库确认失败:$it")
            loading.dismiss()
            showToast(it)
        })
    }

    override fun onResume() {
        super.onResume()
        initData()
    }

    fun initData() {
        loading.show()
        mList.clear()
        page = 1
        mViewModel.getList(page, size)
    }

    override fun click() {
        //上拉加载
        mRefresh.setOnLoadMoreListener {
            page++
            mViewModel.getList(page, size)
        }
        //下拉刷新
        mRefresh.setOnRefreshListener {
            initData()
        }
    }

}